﻿<link href="~/SiteUI/Login.css" rel="stylesheet" />
<div id='LoginForm'>
    <ul id="lstLoginControlls">
        <li><input name="系统帐号" id="txbLoginName" type="text" placeholder="帐号/邮箱/手机" /></li>
        <li><input name="登录密码" id="pwbLoginPass" type="password" placeholder="登录密码" /></li>
        <li><div id="btnLogin">登 录</div></li>
        <li><span id="chkRmbAcnt">记住我的帐号</span></li>
    </ul>
</div>

<script>
    wgLoginForm = {};
    wgLoginForm.frmLogin = {};

    $(function () {
        wgLoginForm.frmLogin._$name = $("#txbLoginName");
        wgLoginForm.frmLogin._$pass = $("#pwbLoginPass");

        //创建标准按钮
        wgLoginForm.frmLogin._btnLogin = new Button($("#btnLogin"));
        wgLoginForm.frmLogin._btnLogin.on("click", FnLogin);

        //创建标准复选框
        wgLoginForm.frmLogin._chkRmbAcnt = new CheckBox($("#chkRmbAcnt"));

        //创建用户名控件的验证规则
        wgLoginForm.frmLogin._validaters = new Validater({ BlurOnError: true,CheckOnBlur:false });
        wgLoginForm.frmLogin._validaters.NoEmpty(wgLoginForm.frmLogin._$name, "", "请填写您的注册帐号");

        //检查Cookie是否已保存上次登录帐号
        var la = $.Cookie("LastAccount");
        if (!ObjHelper.IsEmpty(la)) {
            wgLoginForm.frmLogin._$name.val(la);
            wgLoginForm.frmLogin._$pass.focus();
            wgLoginForm.frmLogin._chkRmbAcnt.val('checked');
        }
        else {
            wgLoginForm.frmLogin._$name.focus();
        }

        //智能化键盘操作
        KeybordHelper.on(wgLoginForm.frmLogin._$name, 'enter', function (ev) { wgLoginForm.frmLogin._$pass.focus(); });
        KeybordHelper.on(wgLoginForm.frmLogin._$pass, 'enter', function (ev) { wgLoginForm.frmLogin._btnLogin.click(); });

    });

    function FnLogin(ev, data) {
        //TODO:输入值验证
        if (!wgLoginForm.frmLogin._validaters.Check()) {
            return;
        }
        var n = wgLoginForm.frmLogin._$name.val();
        var p = wgLoginForm.frmLogin._$pass.val().toString().MD5();

        //禁用登录按钮，防止重复提交
        wgLoginForm.frmLogin._btnLogin.disable();
        
        $.getJSON("/SoProj/Login", { account: n, pass: p }, FnLoginComplete);
    };

    function FnLoginComplete(result) {
        if (result.IsPositive) {
            if (!wgLoginForm.frmLogin._chkRmbAcnt.val() == "checked") {
                $.Cookie("LastAccount", null);
            }
            $("#LoginForm").fadeOut(function () {
                wgLoginForm.frmLogin._validaters.dispose();
                wgLoginForm.frmLogin._btnLogin.dispose();
                wgLoginForm.frmLogin._chkRmbAcnt.dispose();
                wgLoginForm.frmLogin._btnLogin = null;
                wgLoginForm.frmLogin._chkRmbAcnt = null;
                wgLoginForm.frmLogin._validaters = null;

                wgLoginForm.frmLogin._$name = null;
                wgLoginForm.frmLogin._$pass = null;
                if (ObjHelper.IsFunc(wgLoginForm.frmLogin.onLoginOk)) {
                    wgLoginForm.frmLogin.onLoginOk(result);
                }
                wgLoginForm.frmLogin = null;
            });

        }
        else {
            //默认登录失败时恢复登录窗体
            wgLoginForm.frmLogin._btnLogin.enable();
            wgLoginForm.frmLogin._$pass.focus();
            vf_OnLoginFailure();
        }
    };

    function vf_OnLoginFailure() {
        alert("登录失败，请检查用户名和密码");
    };
</script>